<?php
/**
 * 描述
 * Created by Paco
 * Date: 2018/11/15 Time: 18:48
 */

namespace App\Repositories\Product;


use App\Models\Product\Category;
use App\Repositories\Repository;
use Illuminate\Http\Request;

class CategoryRepository extends Repository
{
    private $redis_key;
    private $cat_tree_key;
    private $redis;
    private $lib_cat;

    public function __construct(Request $request, \App\Lib\Utils\Category $cat)
    {
        parent::__construct($request);
        $this->redis = app('phpredis');
        $this->redis_key = config('rediskey.product.category');
        $this->cat_tree_key = config('rediskey.product.tree_category');
        $this->lib_cat = $cat;
    }

    public function list()
    {
        $categorys = $this->redis->get($this->redis_key);
        $categorys = json_decode($categorys, true);
        if (!$categorys) {
            $categorys = Category::query()->orderBy('sort', 'desc')->get()->toArray();
            if ($categorys) {
                $categorys = $this->lib_cat->getTree($categorys);
                //$categorys = $this->lib_cat->setArrayKey($categorys);
                $this->redis->set($this->redis_key, json_encode($categorys));
            }
        }
        return $this->respondWith($categorys);
    }

    public function tree()
    {
        $categorys = $this->redis->get($this->cat_tree_key);
        $categorys = json_decode($categorys, true);
        if (!$categorys) {
            $categorys = Category::query()->orderBy('sort', 'desc')->get()->toArray();
            if ($categorys) {
                $categorys = $this->lib_cat->getDeepTree($categorys, 'pid', 'cat_id');
                //$categorys = $this->lib_cat->setArrayKey($categorys);
                $this->redis->set($this->cat_tree_key, json_encode($categorys));
            }
        }
        return $this->respondWith($categorys);
    }

    public function show($id)
    {
        $categorys = $this->redis->get($this->redis_key);
        if ($categorys) {
            $categorys = json_decode($categorys, true);
            $category = $categorys[$id];
        } else {
            $category = Category::query()->find($id);
        }
        //$data = ["categorys" => $categorys, "category" => $category];
        return $this->respondWith($category);
    }

    public function store($data)
    {
        $data = array_filter($data, 'my_array_filter');
        try {
            Category::query()->create($data);
            $this->redis->del($this->redis_key);
            $this->redis->del($this->cat_tree_key);
            return $this->respondSuccess();
        } catch (\Exception $e) {
            return $this->throwRespondError(10108);
        }
    }

    public function save($data)
    {
        $category = Category::query()->find($data['cat_id']);
        if ($category) {
            try {
                $category->fill($data)->save();
                $this->redis->del($this->redis_key);
                $this->redis->del($this->cat_tree_key);
                return $this->respondSuccess();
            } catch (\Exception $e) {
                return $this->throwRespondError(10106);
            }
        }
        return $this->respondError(10106);
    }

    public function del($id)
    {
        $cat = Category::query()->get()->toArray();
        $had = $this->lib_cat->haveChild($id, $cat);
        if ($had) {
            return $this->throwRespondError(10801);
        }

        try {
            Category::query()->where("cat_id", "=", $id)->delete();
            $this->redis->del($this->redis_key);
            $this->redis->del($this->cat_tree_key);
            return $this->respondSuccess();
        } catch (\Exception $e) {
            return $this->respondError(10107);
        }
    }

    public function treeNameIds($cat_id)
    {
        return $this->respondWith($this->nameIds($cat_id));
    }


    private function nameIds($cat_id)
    {
        $data = [];
        $categorys = $this->redis->get($this->redis_key);
        if ($categorys) {
            $categorys = json_decode($categorys, true);
            $categorys = $this->lib_cat->setArrayKey($categorys);
            if (key_exists($cat_id, $categorys)) {
                $cat = $categorys[$cat_id];
                $ids = explode('-', $cat['path']);
                foreach ($ids as $id) {
                    if (key_exists($id, $categorys)) {

                        $data['template_id'][] = $categorys[$id]['template_id'];
                        $data['name'][]= $categorys[$id]['name'];
                    }
                }
                $data['template_id'][] = $categorys[$cat_id]['template_id'];
                $data['name'][] = $categorys[$cat_id]['name'];
            }
        } else {
            $categorys = Category::query()->orderBy('sort', 'desc')->get()->toArray();
            if ($categorys) {
                $categorys = $this->lib_cat->getTree($categorys);
                //$categorys = $this->lib_cat->setArrayKey($categorys);
                $this->redis->set($this->redis_key, json_encode($categorys));
                $this->nameIds($cat_id);
            }
        }

        return $data;
    }

}